package cn.jlonely.xyhx.admin.controller;

import cn.jlonely.xyhx.pojo.Admin;
import cn.jlonely.xyhx.pojo.vo.AdminVo;
import cn.jlonely.xyhx.admin.service.AdminService;
import cn.jlonely.xyhx.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "*", maxAge = 3600)
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 管理员登录
     * @param admin
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public Result login(@RequestBody Admin admin) throws Exception {
        AdminVo adminVo = adminService.login(admin);
        if (adminVo != null){
            return Result.success(adminVo);
        }
        return Result.error("用户名或密码错误");
    }

    /**
     * 修改管理员信息
     * @param admin
     * @return
     * @throws Exception
     */
    @PutMapping("/updateAdminInfo")
    public Result updateInfo(@RequestBody Admin admin) throws Exception {
        adminService.updateInfo(admin);
        return Result.success();
    }
}
